﻿using System;
using System.ComponentModel;
using System.Data;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.Task.Context
{
    public class User : Crud, ITask.IUser
    {
        public event FormEvent.FormEventHandler OnGetUserId;

        protected void TriggerGetUserId(FormEvent e)
        {
            if (OnGetUserId != null)
                OnGetUserId(this, e);
        }

        public void GetUserId(string username, string password)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwGetUserIdDoWork;
                a.RunWorkerCompleted += BgwGetUserIdWorkerCompleted;
                a.RunWorkerAsync(new object[] {username, password});
            }
        }

        public void GetUserById(UInt64 id)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwFetchDoWork;
                a.RunWorkerCompleted += BgFetchRunWorkerCompleted;
                a.RunWorkerAsync(id);
            }
        }

        #region Protected Backgroud Worker Function
        protected void BgwGetUserIdDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var data = (object[])e.Argument;
                var username = data[0].ToString();
                var password = data[1].ToString();
                e.Result = new model.Table.User().GetUserId(username, password);
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = 0;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }        
        }

        protected void BgwGetUserIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerGetUserId(new FormEvent(!IsError) {Value = e.Result});
            IsError = false;
        }
        #endregion

        #region Protected Overridable Function
        protected override void BgwFetchDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var id = (UInt64)e.Argument;
                e.Result = new model.Table.User().GetUserById(id);
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = new DataTable();
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }      
        }

        protected override void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var result = (DataTable) e.Result;
            TriggerFetch(new FormEvent(!result.Rows.Count.Equals(0))
                             {
                                 Exception =
                                     result.Rows.Count.Equals(0)
                                         ? new Exception("Invalid username or password.")
                                         : null,
                                 Value = result
                             });
            IsError = false;
        }

        protected override void BgwFetchListsDoWork(object sender, DoWorkEventArgs e)
        {
            //e.Result = new Model.Table.Employee().GetEmployeeAll();
        }

        protected override void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //var a = (DataTable)e.Result;
            //var data =
            //    (a.Rows.Cast<DataRow>().Select(
            //        dr =>
            //        new Domain.Employee { Id = (UInt32)dr["ID"], EmployeeName = dr["APPOINTMENT_STATUS_NAME"].ToString() })).
            //        ToList();
            //TriggerFetchLists(this, new FormEvent(!data.Count.Equals(0)) { Value = data });
        }

        protected override void BgwInsertDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwUpdateDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwDeleteDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwToggleDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }
        #endregion
    }
}
